package com.isoft.mapper;

import com.isoft.entity.DfmeLbdeEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface DfmeLbdeMapper {

    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) VALUES(#{entity.meta.sender}, #{entity.meta.receiver}, #{entity.meta.sequenceNumber}, TO_TIMESTAMP(#{entity.meta.dateTime}, 'YYYYMMDDHH24MISS'), #{entity.meta.type}, #{entity.meta.subType})")
    @Options(useGeneratedKeys = true, keyProperty = "meta.msgId", keyColumn = "MSG_ID")
    void insertMeta(@Param("entity") DfmeLbdeEntity entity);

    @Insert("INSERT INTO FLIGHT_BASE(" +
            "FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER, " +
            "STATUS, INTERNAL_STATUS, MANUAL_STATUS, MANUAL_INTERNAL_STATUS) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "CASE WHEN #{dflt.status,jdbcType=VARCHAR} = 'LBD' THEN 'BOARDING_COMPLETED' " +
            "ELSE COALESCE(NULLIF(#{dflt.status,jdbcType=VARCHAR},''), 'NORMAL') END, " +
            "#{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualInternalStatus,jdbcType=VARCHAR})")
    int insertFlightBase(DfmeLbdeEntity entity);

    @Update("UPDATE FLIGHT_BASE SET " +
            "STATUS = CASE WHEN #{dflt.status,jdbcType=VARCHAR} = 'LBD' THEN 'BOARDING_COMPLETED' " +
            "ELSE COALESCE(NULLIF(#{dflt.status,jdbcType=VARCHAR},''), 'NORMAL') END, " +
            "INTERNAL_STATUS = #{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "MANUAL_STATUS = #{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "MANUAL_INTERNAL_STATUS = #{dflt.manualInternalStatus,jdbcType=VARCHAR} " +
            "WHERE FLIGHT_ID = #{dflt.flightId,jdbcType=NUMERIC}")
    int updateFlightBase(DfmeLbdeEntity entity);

    @Insert("INSERT INTO BOARDING_TIME(FLIGHT_ID, BOARDING_TYPE, BOARDING_TIME) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "'LBDE', " +
            "TO_TIMESTAMP(#{dflt.lastBaggageTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))")
    int insertBoardingTime(DfmeLbdeEntity entity);
}